/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Collita;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Produccio;
import entity.UnitatsMesura;

/**
 *
 * @author David
 */
public class CollitaJpaController implements Serializable {

    public CollitaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Collita collita) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Produccio produccioId = collita.getProduccioId();
            if (produccioId != null) {
                produccioId = em.getReference(produccioId.getClass(), produccioId.getId());
                collita.setProduccioId(produccioId);
            }
            UnitatsMesura unitatsMesuraId = collita.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId = em.getReference(unitatsMesuraId.getClass(), unitatsMesuraId.getId());
                collita.setUnitatsMesuraId(unitatsMesuraId);
            }
            em.persist(collita);
            if (produccioId != null) {
                produccioId.getCollitaCollection().add(collita);
                produccioId = em.merge(produccioId);
            }
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getCollitaCollection().add(collita);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Collita collita) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collita persistentCollita = em.find(Collita.class, collita.getId());
            Produccio produccioIdOld = persistentCollita.getProduccioId();
            Produccio produccioIdNew = collita.getProduccioId();
            UnitatsMesura unitatsMesuraIdOld = persistentCollita.getUnitatsMesuraId();
            UnitatsMesura unitatsMesuraIdNew = collita.getUnitatsMesuraId();
            if (produccioIdNew != null) {
                produccioIdNew = em.getReference(produccioIdNew.getClass(), produccioIdNew.getId());
                collita.setProduccioId(produccioIdNew);
            }
            if (unitatsMesuraIdNew != null) {
                unitatsMesuraIdNew = em.getReference(unitatsMesuraIdNew.getClass(), unitatsMesuraIdNew.getId());
                collita.setUnitatsMesuraId(unitatsMesuraIdNew);
            }
            collita = em.merge(collita);
            if (produccioIdOld != null && !produccioIdOld.equals(produccioIdNew)) {
                produccioIdOld.getCollitaCollection().remove(collita);
                produccioIdOld = em.merge(produccioIdOld);
            }
            if (produccioIdNew != null && !produccioIdNew.equals(produccioIdOld)) {
                produccioIdNew.getCollitaCollection().add(collita);
                produccioIdNew = em.merge(produccioIdNew);
            }
            if (unitatsMesuraIdOld != null && !unitatsMesuraIdOld.equals(unitatsMesuraIdNew)) {
                unitatsMesuraIdOld.getCollitaCollection().remove(collita);
                unitatsMesuraIdOld = em.merge(unitatsMesuraIdOld);
            }
            if (unitatsMesuraIdNew != null && !unitatsMesuraIdNew.equals(unitatsMesuraIdOld)) {
                unitatsMesuraIdNew.getCollitaCollection().add(collita);
                unitatsMesuraIdNew = em.merge(unitatsMesuraIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = collita.getId();
                if (findCollita(id) == null) {
                    throw new NonexistentEntityException("The collita with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collita collita;
            try {
                collita = em.getReference(Collita.class, id);
                collita.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The collita with id " + id + " no longer exists.", enfe);
            }
            Produccio produccioId = collita.getProduccioId();
            if (produccioId != null) {
                produccioId.getCollitaCollection().remove(collita);
                produccioId = em.merge(produccioId);
            }
            UnitatsMesura unitatsMesuraId = collita.getUnitatsMesuraId();
            if (unitatsMesuraId != null) {
                unitatsMesuraId.getCollitaCollection().remove(collita);
                unitatsMesuraId = em.merge(unitatsMesuraId);
            }
            em.remove(collita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Collita> findCollitaEntities() {
        return findCollitaEntities(true, -1, -1);
    }

    public List<Collita> findCollitaEntities(int maxResults, int firstResult) {
        return findCollitaEntities(false, maxResults, firstResult);
    }

    private List<Collita> findCollitaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Collita.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Collita findCollita(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Collita.class, id);
        } finally {
            em.close();
        }
    }

    public int getCollitaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Collita> rt = cq.from(Collita.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
